<?php

namespace Index\Controller;
use Think\Controller;
use Org\Util\Wechat;
use Think\Log;

class ApiController extends Controller{
	
	public $wx = null;
	
	public $config = array(
		'appid'         =>'wx7197628dc9966e3d',
		'token'         =>'zhijin101',
		'encodingaeskey'=>'CvECAzjZteRdhjTinszJesrxkyhXKGfstSSGgEKahSe',
		'appsecret'     =>'2df3acd8ec9b083b374fedf18fa723e5',
		'debug'         =>false,
	);
	
	public function _initialize(){  
		$this->wx = new Wechat($this->config);
	}
	
	
	public function index(){ 
		$this->wx->valid();
		$this->wx->checkAuth();
		$this->wx->getRev()->getRevFrom(); 
	
		//获取事件类型
		$type = $this->wx->getRev()->getRevType();
		switch ($type) {
			case Wechat::MSGTYPE_TEXT:	//文字
				$this->onTextEvent();
				break;
			case Wechat::MSGTYPE_IMAGE:	//图片
				$this->onImageEvent();
				break;
			case Wechat::MSGTYPE_LOCATION:	//位置
				$this->onLocationEvent();
				break;
			case Wechat::MSGTYPE_LINK:	//链接
				$this->onLinkEvent();
				break;
			case Wechat::MSGTYPE_EVENT:  //事件
				$this->onEventEvent();
				break;
			case Wechat::MSGTYPE_MUSIC:	//音乐 
				$this->onMusicEvent();
				break;
			case Wechat::MSGTYPE_NEWS:	//新闻
				$this->onNewsEvent();
				break;
			case Wechat::MSGTYPE_VOICE:	//声音
				$this->onVoiceEvent();
				break;
			case Wechat::MSGTYPE_VIDEO:	//视频
				$this->onVideoEvent();
				break;
			default :
				$wx->text('END')->reply();
				break;
		}
	}
	
	public function onTextEvent(){
		$Rev = $this->wx->getRevData();
		Log::record(json_encode($Rev),'WARN',true); 
		$content = $Rev['Content']; 
		if(strpos($content,'1')>=0 || strpos($content,'一')>=0){
			$rs = $this->createKfSession($this->wx->getRevFrom(),'kf2002@zj_wgj');
			Log::record(json_encode($rs),'WARN',true);
			$this->wx->text('已为你连接客服一')->reply();
		}elseif(strpos($content,'2')>=0 || strpos($content,'二')>=0){ 
			$rs = $this->createKfSession($this->wx->getRevFrom(),'kf2003@zj_wgj');
			Log::record(json_encode($rs),'WARN',true);
			$this->wx->text('已为你连接客服二')->reply();
		}else{
			$rs = $this->createKfSession($this->wx->getRevFrom(),'kf2005@zj_wgj');
			Log::record(json_encode($rs),'WARN',true);
			$this->wx->text('已为你连接默认客服')->reply();
		} 
	}
	public function onImageEvent(){
		$this->wx->text('onImageEvent')->reply();
		
	}
	public function onLocationEvent(){
		$this->wx->text('onLocationEvent')->reply();
		
	}
	public function onLinkEvent(){
		$this->wx->text('onLinkEvent')->reply();
		
	}
	public function onEventEvent(){
		$this->wx->text('onEventEvent')->reply();
		
	}
	public function onMusicEvent(){
		$this->wx->text('onMusicEvent')->reply();
		
	}
	public function onNewsEvent(){
		$this->wx->text('onNewsEvent')->reply();
		
	}
	public function onVoiceEvent(){ 
		$this->wx->text('onVoiceEvent')->reply();
		exit();
	}
	
	function createKfSession($openid,$kfid){
		$this->wx->createKFSession($openid,$kfid,'成功连接客服');
		Log::record(json_encode(array($openid,$kfid,'成功连接客服')),'WARN',true);
	}
	
	public function onVideoEvent(){
		$this->wx->text('onVideoEvent')->reply();
		
	}
	
	public function test(){  
		$rs = $this->wx->getCustomServiceKFlist(); 
		P($this->wx,false);
		P($rs);
	}
		
}